using Microsoft.AspNetCore.Mvc;
using RuoVea.OmiApi.UserRole.Service;
using RuoVea.OmiApi.UserRole.Service.Dto;
using RuoVea.OmiUserRole.Attributes;

namespace RuoVea.OmiUserRole.Controllers;
/// <summary>
/// 
/// </summary>
public class PageUserInputEx : PageUserInput
{
    private int _currentPage;
    /// <summary>
    /// 
    /// </summary>
    public int CurrentPage
    {
        get { return _currentPage; }
        set
        {
            _currentPage = value;
            base.PageNo = value;
        }
    }
}
/// <summary>
/// 用户 
/// </summary> 
[AppRoute]
public class UserController : Controller
{
    private readonly SysUserService _sysUserService;
    private readonly SysUserRoleService _sysUserRoleService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sysUserService"></param>
    /// <param name="sysUserRoleService"></param>
    public UserController(SysUserService sysUserService, SysUserRoleService sysUserRoleService)
    {
        _sysUserService = sysUserService;
        _sysUserRoleService = sysUserRoleService;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [HttpGet(nameof(Index))]
    public IActionResult Index() { return View(); }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [HttpGet("userrole/{id:long}")]
    public IActionResult UserRole(long id) { ViewBag.Id = id; return View(); }

    /// <summary>
    /// 分页查询用户
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("pages")]
    public async Task<RestfulResult<PageResult<UserOutput>>> GetPagesAsync([FromBody] PageUserInputEx data)
    {
        RestfulResult<PageResult<UserOutput>> restfulResult = new() { Code = CodeStatus.OK };
        restfulResult.Data = await _sysUserService.Page(data);
        return restfulResult;
    }

    /// <summary>
    /// 增加用户
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("add")]
    public async Task<RestfulResult> AddAsync(AddUserInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            data.AdminType = AdminType.None;
            long result = await _sysUserService.AddUser(data);
            if (result > 0) return restfulResult;
            else
            {
                restfulResult.Code = CodeStatus.BadRequest;
                restfulResult.Data = false;
                return restfulResult;
            }
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 更新用户
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("update")]
    public async Task<RestfulResult> UpdateUser(UpdateUserInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            await _sysUserService.UpdateUser(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 删除用户
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("delete")]
    public async Task<RestfulResult> DeleteAsync(DeleteUserInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            await _sysUserService.DeleteUser(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 修改用户状态
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost(nameof(ChangeUserStatus))]
    public async Task<RestfulResult> ChangeUserStatus(UserInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            long result = await _sysUserService.SetStatus(data);
            if (result > 0) return restfulResult;
            else
            {
                restfulResult.Code = CodeStatus.BadRequest;
                restfulResult.Data = false;
                return restfulResult;
            }
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 重置用户密码
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost(nameof(ResetUserPwd))]
    public async Task<RestfulResult> ResetUserPwd([FromBody] ResetPwdUserInput data)
    {
        RestfulResult restfulResult = new() { Code = CodeStatus.OK, Data = true };
        try
        {
            string result = await _sysUserService.ResetPwd(data);
            return restfulResult;
        }
        catch (Exception ex)
        {
            restfulResult.Code = CodeStatus.BadRequest;
            restfulResult.Data = false;
            restfulResult.Message = ex.Message;
            return restfulResult;
        }
    }

    /// <summary>
    /// 授权用户角色
    /// </summary>
    /// <param name="data">角色Id列表</param>
    /// <returns></returns>
    [HttpPost("grantuserroles")]
    public async Task<RestfulResult> GrantUserRoles(UserRolesInput data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        var user = await _sysUserService.GetUserById(data.UserId);
        if (user.AdminType == AdminType.SuperAdmin)
        {
            restful.Message = i18n.prohibit_assign_role_to_admin;
            return restful;
        }
        if (user.AdminType == AdminType.Admin)
        {
            restful.Message = ErrorEnum.D1008.GetDescription();
            return restful;
        }
        try
        {
            await _sysUserRoleService.GrantUserRoles(data); 
            restful.Code = CodeStatus.OK;
            restful.Data = true;
            return restful;
        }
        catch (Exception ex)
        {
            restful.Code = CodeStatus.BadRequest;
            restful.Data = false;
            restful.Message = ex.Message;
            return restful;
        }
    }
    /// <summary>
    /// 移除用户的角色
    /// </summary>
    /// <param name="data">角色Id列表</param>
    /// <returns></returns>
    [HttpPost("removeuserroles")]
    public async Task<RestfulResult> RemoveUserRoles(UserRolesInput data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        var user = await _sysUserService.GetUserById(data.UserId);
        if (user.AdminType == AdminType.SuperAdmin)
        {
            restful.Message = i18n.prohibit_assign_role_to_admin;
            return restful;
        }
        if (user.AdminType == AdminType.Admin)
        {
            restful.Message = ErrorEnum.D1008.GetDescription();
            return restful;
        }
        try
        {
            await _sysUserRoleService.DeleteUserRoleByUserIdRoleIds(data);
            restful.Code = CodeStatus.OK;
            restful.Data = true;
            return restful;
        }
        catch (Exception ex)
        {
            restful.Code = CodeStatus.BadRequest;
            restful.Data = false;
            restful.Message = ex.Message;
            return restful;
        }
    }

    /// <summary>
    /// 获取用户列表
    /// </summary>
    /// <returns></returns>
    [HttpGet("getsysuserlist")]
    public async Task<RestfulResult<List<UserOutput>>> GetSysUserList()
    {
        RestfulResult<List<UserOutput>> restful = new() { Code = CodeStatus.OK };
        try
        {
            restful.Data = await _sysUserService.UserList();
            return restful;
        }
        catch (Exception ex)
        {
            restful.Code = CodeStatus.BadRequest;
            restful.Message = ex.Message;
            return restful;
        }
    }

    /// <summary>
    /// 获取角色用户相关信息 
    /// </summary>
    /// <param name="roleId"></param>
    /// <returns></returns>
    [HttpGet("roleuserlistbyroleid/{roleId:long}")]
    public async Task<RestfulResult<List<UserOutput>>> GetRoleUserListByRoleId(long roleId)
    {
        RestfulResult<List<UserOutput>> restful = new() { Code = CodeStatus.OK };
        restful.Data = await _sysUserService.GetRoleUserListByRoleId(roleId);
        return restful;
    }
}

